package com.dianwoba.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;


public class SystemUtil {
	
	private static final int BYTE_ARRAY_SIZE = 8 * 1024;
	
	/**
	 * 获得超类的参数类型，取第一个参数类型
	 * @param <T> 类型参数
	 * @param clazz 超类类型
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static <T> Class<T> getClassGenricType(final Class clazz) {
		return getClassGenricType(clazz, 0);
	}
	
	/**
	 * 根据索引获得超类的参数类型
	 * @param clazz 超类类型
	 * @param index 索引
	 */
	@SuppressWarnings("rawtypes")
	public static Class getClassGenricType(final Class clazz, final int index) {
		Type genType = clazz.getGenericSuperclass();
		if (!(genType instanceof ParameterizedType)) {
			return Object.class;
		}
		Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
		if (index >= params.length || index < 0) {
			return Object.class;
		}
		if (!(params[index] instanceof Class)) {
			return Object.class;
		}
		return (Class) params[index];
	}
	
	
	/**
	 * 列表对象深拷贝
	 * @param <T>	列表实体类型
	 * @param src	拷贝源
	 * @return		拷贝目标
	 */
    @SuppressWarnings("unchecked")
	public static <T> List<T> deepCopy(List<T> src) {

	 	try{
		 
			 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();   
			 ObjectOutputStream out = new ObjectOutputStream(byteOut);   
			 out.writeObject(src);   
		
			 ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());   
			 ObjectInputStream in =new ObjectInputStream(byteIn);   
			 List<T> dest = (List<T>)in.readObject();   
			 return dest;   		 
	
		 }catch(Exception e){
			 e.printStackTrace();
			 return null;
		 }

	 }   
    
    public static Object deepCopy(Object src){
    	
    	try{
   		 
			 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();   
			 ObjectOutputStream out = new ObjectOutputStream(byteOut);   
			 out.writeObject(src);   
		
			 ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());   
			 ObjectInputStream in =new ObjectInputStream(byteIn);   
			 Object dest = in.readObject();   
			 return dest;   		 
	
		 }catch(Exception e){
			 e.printStackTrace();
			 return null;
		 }
    	
    	
    }
    
    /**
     * 将对象序列化
     * 
     * @param o
     * @return
     */
    public static byte[] fromObjectToBinary(Object o) {

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(
                    BYTE_ARRAY_SIZE);
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            try {
                oos.writeObject(o);
            } finally {
                oos.close();
            }
            return baos.toByteArray();
        } catch (IOException ex) {
        	ex.printStackTrace();
            return null;
        }
    }
    
    /**
     * 序列对象化
     * 
     * @param binary
     * @return
     */
    public static Object fromBinaryToObject(byte[] binary) {
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(binary);
            ObjectInputStream ois = new ObjectInputStream(bais);
            try {
                return ois.readObject();
            } finally {
                ois.close();
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
            return null;
        }
    }
}
